<template>

  <div class="container"
       ref="containerRef">
    <div class="swiper">
        <tiny-carousel height="50vw" show-title>
    <tiny-carousel-item class="carousel-item-demo" v-for="(item,idx) in swiper" :key="idx" :title="item.title" @click="router.push('/swiper/'+item.id)">
      <img :src="item.cover" alt="" style="object-fit: cover;width: 100%;height: 100%">
    </tiny-carousel-item>
  </tiny-carousel>
    </div>
    <div class="option">
      <van-tabs v-model:active="active" @click-tab="onClickTab">
        <van-tab title="离线"></van-tab>
        <van-tab title="在线"></van-tab>
         <van-tab title="VR"></van-tab>
      </van-tabs>
    </div>

    <div class="good" >
      <div class="item" v-for="i in goods" @click="navTo(i)">
        <div class="img">
          <van-image
              width="100%"
              height="100%"
              :src="i.img"
          />
        </div>
        <div class="name">{{ i.name }}</div>
        <div class="btm">
          <van-button style="height: 25px;font-size: 10px;width: 50px; padding: 1px!important;"
                      :type="i.count==0?'danger':'primary'">{{ i.count == 0 ? '使用中' : '空闲中' }}
          </van-button>
          <div></div>
          <div class="icon">
            <van-icon name="fire-o" color="#ee0a24"/>
            {{ i.download }}
          </div>
          <div class="icon">
            <van-icon name="eye-o"/>
            {{ i.visit }}
          </div>
        </div>
      </div>
      <div class="page"
           style="background-color: transparent;position:absolute; bottom: -20px;left:50%;transform: translateX(-50%)">
        <van-pagination :force-ellipses="true" v-model="currentPage" :total-items="total" :show-page-size="8"
                        @change="change">
          <template #prev-text>
            <van-icon name="arrow-left"/>
          </template>
          <template #next-text>
            <van-icon name="arrow"/>
          </template>
          <template #page="{ text }">{{ text }}</template>
        </van-pagination>

      </div>
    </div>

  </div>
</template>

<script setup>
import {ref, onMounted, inject} from "vue";
import {useRouter} from "vue-router";
import {showNotify} from "vant";
import { Carousel as TinyCarousel, CarouselItem as TinyCarouselItem } from '@opentiny/vue'
const currentPage = ref(0)
// 获取 body 元素
const body = document.body;

// 添加滚轮事件监听器
body.addEventListener('wheel', function(event) {
 const distanceToBottom = document.documentElement.scrollHeight - (window.scrollY + window.innerHeight);
    if (distanceToBottom<100) {

    if (flag.value && isBottom.value) {
      flag.value = false
      setTimeout(() => {
        load()
      }, 500)
    }

  }
});

// 如果你需要在某个时刻移除监听器，可以这样做：
// body.removeEventListener('wheel', yourEventListenerFunction);

const p = ref(0)
const isBottom = ref(true)
const axios = inject("axios")
const router = useRouter()

const flag = ref(true)
const total = ref(0)
const kind = ref("")
//每页size
const s = ref(8)
const containerRef = ref(null)
const active = ref(0)
const goods = ref([])
const swiper = ref([])
const navTo = e => {
  axios.get('goods/visit?id=' + e.id).catch(err => showNotify({type: 'danger', message: "网络异常"})
  )
  router.push('/item/' + e.id)
}

const onClickTab = (e) => {
  kind.value = e.title
  p.value = 0
  axios.get('goods/list?kind=' + e.title + '&p=0&s=' + s.value).then(res => {
    goods.value = res.data.data.data
    total.value = res.data.data.p
    containerRef.value.scrollTop = 0
  }).catch(err => {
        showNotify({type: 'danger', message: "网络异常"})
      }
  )
}

onMounted(() => {
  axios.get('goods/list?kind=离线&p=0&s=' + s.value).then(res => {
    goods.value = res.data.data.data
    total.value = res.data.data.p
  }).catch(err => {
        showNotify({type: 'danger', message: "网络异常"})
      }
  )
    axios.get('swiper/get').then(res => {
    swiper.value = res.data.data
  }).catch(err => {
      }
  )
})
const load = () => {
  p.value = p.value + 1
  const page = Math.ceil(total.value / s.value)
  if (p.value < page)
    axios.get('goods/list?kind=' + kind.value + '&p=' + p.value + '&s=' + s.value).then(res => {
      res.data.data.data.forEach(i => {
        goods.value.push(i)
      })
      total.value = res.data.data.p
    }).catch(err => {
          showNotify({type: 'danger', message: "网络异常"})
        }
    )
  else {
    showNotify({type: 'warning', message: "我们也是有底线的"})
    isBottom.value = false
  }
}

const change = () => {
  axios.get('goods/list?kind=' + kind.value + '&p=' + currentPage.value + '&s=' + 8).then(res => {
    goods[0] = res.data.data.data
  })

}
</script>

<style scoped lang="less">
.container{
  width: 100%;
  padding-bottom: 10px;
  height:auto;
  .tag {
    position: absolute;
    right: 10px;
    clip-path: polygon(25% 0%, 100% 1%, 100% 100%, 25% 100%, 0% 50%);
    display: block;
    z-index: 10;
    width: 70px;
    height: 20px;
    line-height: 20px;
    font-size: 11px;
    color: #ffffff;
    text-indent: 1em;
    text-align: start;
    top: 9px;
    background-color: red;
  }
  margin: auto;
  .option {
    margin: 10px;
    width: 300px;
    padding: 10px;
    border-radius: 10px;
  }

  .good {
    display: flex;
    flex-wrap: wrap;
    margin: 10px auto;

    .item {
      position: relative;
      box-sizing: border-box;
      padding: 10px;
      background-color: #ffffff;
      border-radius: 10px;
      margin-bottom: 20px;
      .img {
        border-radius: 10px;
        overflow: hidden;
      }

      font-size: 12px;

      .name {
        margin-top: 10px;
        line-height: 15px;
        color: #606266;
        font-weight: 500;
        font-size: 12px;
        text-align: start;
        height: 30px;
        font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
      }

      .btm {
        margin: 10px 0;
        display: flex;
        justify-content: space-between;
        align-items: center;

        .icon {
          margin-right: 10px;
        }
      }
    }


  }


}


@media screen and(min-height: 480px) {
  .container {
   // overflow: hidden;

  }

  .page {
    width: auto;
    margin: 0 auto;
    text-align: center;
    transform: translateY(-40px);
    background-color: #f5f6f7;
  }


  .good {
    justify-content: space-between;
    width: 9.6rem;

    .item {
      width: 2.3rem;
      height: 18vm;
      max-width: 270px;

      .img {
        width: 98%;
        height: 10vw;
      }

    }
  }
}

@media screen and(max-width: 480px) {
  .container {
   // height: 92vh !important;
  }

  .page,
  .float {
    display: none;
  }

  .good {
    justify-content: space-between;
    width: 96vw;
    padding-bottom: 10px;
    height: auto;

    .img {
      width: 98%;
      height: 19vw !important;
    }

    .item {
      width: 43vw;
      height: 43vw;
    }
  }
}
</style>